package dS.ProjectGPS.GPS;

import dS.ProjectGPS.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.MessageConnection;
import javax.microedition.io.Connector;
import javax.wireless.messaging.TextMessage;

public class FormSMSSendLog extends Form {

  StringItem note = new StringItem("",
                                   "NOTE: SMS limits 160 characters per message. If over this would be sent as multiple messages");
  TextField number = new TextField("To:", Options.phNumber, 20,
                                   TextField.NUMERIC);

  String log = "\n";

  StringItem message = null;
  TextField extra = new TextField("Note:", "", 50, TextField.ANY);

  Ticker ticker1 = new Ticker("Send your location details to..");

  public FormSMSSendLog() {
    super("Send Log");

    for (int i = 0; i < NMEAReader.logPosition.size(); i++) {
      Position current = (Position) NMEAReader.logPosition.elementAt(i);
      log += "" + i + " " + current.lat + " " + current.lon + '\n';
    }

    message = new StringItem("Data:", log);

    try {
      NDSInit();
    }
    catch (Exception ex) {
    }
  }

  public void NDSInit() throws Exception {
    append(note);
    append(number);
    append(extra);
    append(message);
    setTicker(ticker1);
  }

  public void sendSMS() {
    String recipient = number.getString();
    Options.phNumber = recipient;
    String address = "sms://" + recipient;

    MessageConnection smsconn = null;
    try {
      //Open the message connection.
      smsconn = (MessageConnection) Connector.open(address);
      TextMessage txtMessage = (TextMessage) smsconn.newMessage(
         MessageConnection.TEXT_MESSAGE);
      txtMessage.setPayloadText(extra.getString() + log);
      smsconn.send(txtMessage);
      smsconn.close();
    }
    catch (Exception e) {
    }
  }
}
